package com.easymeeting.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.easymeeting.entity.dto.MessageSendDto;
import com.easymeeting.entity.enums.*;
import com.easymeeting.entity.po.UserContact;
import com.easymeeting.entity.query.UserContactQuery;
import com.easymeeting.exception.BusinessException;
import com.easymeeting.mappers.UserContactMapper;
import com.easymeeting.websocket.message.MessageHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.easymeeting.entity.query.UserContactApplyQuery;
import com.easymeeting.entity.po.UserContactApply;
import com.easymeeting.entity.vo.PaginationResultVO;
import com.easymeeting.entity.query.SimplePage;
import com.easymeeting.mappers.UserContactApplyMapper;
import com.easymeeting.service.UserContactApplyService;
import com.easymeeting.utils.StringTools;


/**
 * 联系人申请 业务接口实现
 */
@Service("userContactApplyService")
public class UserContactApplyServiceImpl implements UserContactApplyService {

    @Resource
    private UserContactApplyMapper<UserContactApply, UserContactApplyQuery> userContactApplyMapper;
    @Resource
    private UserContactMapper<UserContact, UserContactQuery> userContactMapper;
    @Autowired
    private MessageHandler messageHandler;

    /**
     * 根据条件查询列表
     */
    @Override
    public List<UserContactApply> findListByParam(UserContactApplyQuery param) {
        return this.userContactApplyMapper.selectList(param);
    }

    /**
     * 根据条件查询列表
     */
    @Override
    public Integer findCountByParam(UserContactApplyQuery param) {
        return this.userContactApplyMapper.selectCount(param);
    }

    /**
     * 分页查询方法
     */
    @Override
    public PaginationResultVO<UserContactApply> findListByPage(UserContactApplyQuery param) {
        int count = this.findCountByParam(param);
        int pageSize = param.getPageSize() == null ? PageSize.SIZE15.getSize() : param.getPageSize();

        SimplePage page = new SimplePage(param.getPageNo(), count, pageSize);
        param.setSimplePage(page);
        List<UserContactApply> list = this.findListByParam(param);
        PaginationResultVO<UserContactApply> result = new PaginationResultVO(count, page.getPageSize(), page.getPageNo(), page.getPageTotal(), list);
        return result;
    }

    /**
     * 新增
     */
    @Override
    public Integer add(UserContactApply bean) {
        return this.userContactApplyMapper.insert(bean);
    }

    /**
     * 批量新增
     */
    @Override
    public Integer addBatch(List<UserContactApply> listBean) {
        if (listBean == null || listBean.isEmpty()) {
            return 0;
        }
        return this.userContactApplyMapper.insertBatch(listBean);
    }

    /**
     * 批量新增或者修改
     */
    @Override
    public Integer addOrUpdateBatch(List<UserContactApply> listBean) {
        if (listBean == null || listBean.isEmpty()) {
            return 0;
        }
        return this.userContactApplyMapper.insertOrUpdateBatch(listBean);
    }

    /**
     * 多条件更新
     */
    @Override
    public Integer updateByParam(UserContactApply bean, UserContactApplyQuery param) {
        StringTools.checkParam(param);
        return this.userContactApplyMapper.updateByParam(bean, param);
    }

    /**
     * 多条件删除
     */
    @Override
    public Integer deleteByParam(UserContactApplyQuery param) {
        StringTools.checkParam(param);
        return this.userContactApplyMapper.deleteByParam(param);
    }

    /**
     * 根据ApplyId获取对象
     */
    @Override
    public UserContactApply getUserContactApplyByApplyId(Integer applyId) {
        return this.userContactApplyMapper.selectByApplyId(applyId);
    }

    /**
     * 根据ApplyId修改
     */
    @Override
    public Integer updateUserContactApplyByApplyId(UserContactApply bean, Integer applyId) {
        return this.userContactApplyMapper.updateByApplyId(bean, applyId);
    }

    /**
     * 根据ApplyId删除
     */
    @Override
    public Integer deleteUserContactApplyByApplyId(Integer applyId) {
        return this.userContactApplyMapper.deleteByApplyId(applyId);
    }

    /**
     * 根据ApplyUserIdAndReceiveUserId获取对象
     */
    @Override
    public UserContactApply getUserContactApplyByApplyUserIdAndReceiveUserId(String applyUserId, String receiveUserId) {
        return this.userContactApplyMapper.selectByApplyUserIdAndReceiveUserId(applyUserId, receiveUserId);
    }

    /**
     * 根据ApplyUserIdAndReceiveUserId修改
     */
    @Override
    public Integer updateUserContactApplyByApplyUserIdAndReceiveUserId(UserContactApply bean, String applyUserId, String receiveUserId) {
        return this.userContactApplyMapper.updateByApplyUserIdAndReceiveUserId(bean, applyUserId, receiveUserId);
    }

    /**
     * 根据ApplyUserIdAndReceiveUserId删除
     */
    @Override
    public Integer deleteUserContactApplyByApplyUserIdAndReceiveUserId(String applyUserId, String receiveUserId) {
        return this.userContactApplyMapper.deleteByApplyUserIdAndReceiveUserId(applyUserId, receiveUserId);
    }

    @Override
    public Integer saveUserContactApply(UserContactApply userContactApply) {
        UserContact userContact = userContactMapper.selectByUserIdAndContactId(userContactApply.getReceiveUserId(), userContactApply.getApplyUserId());
        if (userContact != null && UserContactApplyStatusEnum.BLACKLIST.getStatus().equals(userContact.getStatus())) {
            throw new BusinessException("对方已帮你拉黑");
        }
        //你拉黑了对方，但是你反悔了，要把别人加回来
        if (userContact != null && UserContactStatusEnum.FRIEND.getStatus().equals(userContact.getStatus())) {
            UserContact updateInfo = new UserContact();
            updateInfo.setStatus(UserContactStatusEnum.FRIEND.getStatus());
            updateInfo.setLastUpdateTime(new Date());
            userContactMapper.updateByUserIdAndContactId(updateInfo, userContactApply.getApplyUserId(), userContactApply.getReceiveUserId());
            return UserContactStatusEnum.FRIEND.getStatus();
        }

        UserContactApply apply = userContactApplyMapper.selectByApplyUserIdAndReceiveUserId(userContactApply.getApplyUserId(), userContactApply.getReceiveUserId());
        //空就说明是第一次
        if (apply == null) {
            userContactApply.setStatus(UserContactApplyStatusEnum.INIT.getStatus());
            userContactApply.setLastApplyTime(System.currentTimeMillis());
            userContactApplyMapper.insert(userContactApply);
            //非空就说明是之前添加过被删了
        } else {
            UserContactApply updateinfo = new UserContactApply();
            updateinfo.setStatus(UserContactApplyStatusEnum.INIT.getStatus());
            updateinfo.setLastApplyTime(System.currentTimeMillis());
            userContactApplyMapper.updateByApplyId(updateinfo, userContactApply.getApplyId());
        }

        //发送消息
        MessageSendDto messageSendDto = new MessageSendDto();
        messageSendDto.setMessageSend2Type(MessageSend2TypeEnum.USER.getType());
        messageSendDto.setMessageType(MessageTypeEnum.USER_CONTACT_APPLY.getType());
        messageSendDto.setReceiveUserId(userContactApply.getReceiveUserId());
        messageHandler.sendMessage(messageSendDto);
        return UserContactApplyStatusEnum.INIT.getStatus();
    }

    @Override
    public void dealWithApply(String applyUserId, String userId, String nickName, Integer status) {
        UserContactApplyStatusEnum statusEnum = UserContactApplyStatusEnum.getByStatus(status);
        if (statusEnum == null || statusEnum == UserContactApplyStatusEnum.INIT) {
            throw new BusinessException(ResponseCodeEnum.CODE_600);
        }
        UserContactApply userContactApply = userContactApplyMapper.selectByApplyUserIdAndReceiveUserId(applyUserId, userId);
        if (userContactApply == null) {
            throw new BusinessException(ResponseCodeEnum.CODE_600);
        }

        //添加
        if (UserContactApplyStatusEnum.PASS.getStatus().equals(status)) {
            Date now = new Date();
            UserContact userContact = new UserContact();
            userContact.setUserId(applyUserId);
            userContact.setContactId(userId);
            userContact.setStatus(UserContactStatusEnum.FRIEND.getStatus());
            userContact.setLastUpdateTime(now);
            userContactMapper.insertOrUpdate(userContact);
            //同意后双方都要添加
            userContact.setContactId(applyUserId);
            userContact.setUserId(userId);
            userContactMapper.insertOrUpdate(userContact);
        }

        UserContactApply updateUserContactApply = new UserContactApply();
        updateUserContactApply.setStatus(UserContactApplyStatusEnum.PASS.getStatus());
        userContactApplyMapper.updateByApplyUserIdAndReceiveUserId(updateUserContactApply, applyUserId, userId);

        //发消息
        MessageSendDto sendDto = new MessageSendDto();
        sendDto.setMessageType(MessageTypeEnum.USER_CONTACT_DEAL_WITH.getType());
        sendDto.setMessageSend2Type(MessageSend2TypeEnum.USER.getType());
        sendDto.setReceiveUserId(applyUserId);
        sendDto.setSendUserNickName(nickName);
        sendDto.setMessageContent(status);
        messageHandler.sendMessage(sendDto);

    }
}